﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Golos.Users.UserCache;
using Golos.Web.Utils;

namespace Golos.Web.handlers
{
    public class AttachUserHandler:HandlerBase
    {
        public AttachUserHandler(HttpContext httpContext)
            : base(httpContext)
        {
        }
        public override void Handle()
        {
            try
            {
                Token token = TokenUtils.FromCookies(_httpContext.Request.Cookies);

                string net = GetQueryParam("net");
                string uid = GetQueryParam("uid");

                bool activityToSocialWeb = false;

                //if (GetQueryParam("sharing") == "1")
                //{
                //    activityToSocialWeb = true;
                //}
                
                UserIdType usType = new UserIdType();
                switch (net)
                {
                    case "vk":
                        usType = UserIdType.VK;
                        break;
                    case "fb":
                        usType = UserIdType.FB;
                        break;
                    case "tw":
                        usType = UserIdType.TW;
                        break;
                    case "mailru":
                        usType = UserIdType.MR;
                        break;
                    default:
                        throw new Exception("Некорректный параметр net в запросе. net=" + net);
                }

                //UsersClient.AttachUser(token.User, usType, uid, activityToSocialWeb);

                LogClass.WriteDebugEntry("User: " + token.User + ". Привязываю аккаунт " + usType + ", uid=" + uid);
                _httpContext.Response.Write("{ \"success\": \"Аккаунт привязан.\" }");

                
            }
            catch (Exception e)
            {
                LogClass.WriteErrorEntry(e.ToString());
                _httpContext.Response.Write("{ \"error\": \"Не удалось привязать аккаунт.\" }");
            }
        }
    }
}